package com.orangelife.mobile.widget.imagegridview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.curry.android.R;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 * 自定义GridView
 *
 */
public class ImageGridView extends GridView {

	
	private ArrayList<String> myList;
	private Context mContext=null;
	private ImageDownloader image = ImageDownloader.getImageDownloader();
	private String keyName=null;
	
	
	public ImageGridView(Context context){
		super(context);
	}
	public ImageGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ImageGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	public void init(Context context,ArrayList<String> list){
		this.myList=list;
		this.mContext=context;
		this.setAdapter(new GridViewAdapter());
		
	}

	/**
	 * 
	 * @param context
	 */
	public void init(Context context,String str,String name){
		this.mContext=context;
		String []urls=str.split(name);
		myList=new ArrayList<String>();

		for(int i=0;i<urls.length;i++){
			if(!urls[i].equals(""))
				myList.add(urls[i]);
		}
		
		
		this.setAdapter(new GridViewAdapter());
		
	}
	
	
	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}
	class GridViewAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return myList.size();
		}

		@Override
		public Object getItem(int position) {
			return myList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder=null;
			if(convertView==null){
				viewHolder=new ViewHolder();
				convertView=LayoutInflater.from(mContext).inflate(R.layout.item_image, null);
				viewHolder.imageView=(ImageView)convertView.findViewById(R.id.iv_image);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
		
			String url=myList.get(position);
			System.out.print(viewHolder.imageView);
			image.download(url,
					viewHolder.imageView,ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
			viewHolder.imageView.setOnClickListener(new onClickLinstener(position));
			return convertView;
		}
		
		class onClickLinstener implements OnClickListener{
			int position=0;
			public onClickLinstener(int position){
				this.position=position;
			}
			@Override
			public void onClick(View arg0) {
				imageBrower(position,myList);
			}
			
		}
		class ViewHolder{
			private ImageView imageView; 
		}
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int expandSpec = MeasureSpec.makeMeasureSpec(
				Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}



	



	
}
